package com.example.android_calculator

import android.graphics.Insets.add
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.animation.AnimationUtils
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
import org.jetbrains.annotations.TestOnly
import java.lang.Character.isDigit
import java.math.BigDecimal
import java.util.*
import kotlin.math.log

class MainActivity : AppCompatActivity(), View.OnClickListener {
    var c=Caculate()
    var list= mutableListOf<Char>();

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        clear.setOnClickListener(this)
        delete.setOnClickListener(this)
        percent.setOnClickListener(this)
        div.setOnClickListener(this)
        seven.setOnClickListener(this)
        eight.setOnClickListener(this)
        nine.setOnClickListener(this)
        mutiple.setOnClickListener(this)
        four.setOnClickListener(this)
        five.setOnClickListener(this)
        six.setOnClickListener(this)
        subtract.setOnClickListener(this)
        one.setOnClickListener(this)
        two.setOnClickListener(this)
        three.setOnClickListener(this)
        add.setOnClickListener(this)
        zero.setOnClickListener(this)
        point.setOnClickListener(this)
        equal.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
       when(v?.id)
       {
           R.id.clear->{
               result.text="";
               expression.text="";
                list.clear()
           }
           R.id.delete->{

                   if(list.size!=0)
                   {
                       list.removeAt(list.size-1);
                       var s=expression.text
                       s=s.substring(0,s.length-1)
                       expression.text=s
                       result.text=""
                   }


           }

           R.id.percent->{


//               list.add('%')
//               expression.append("%")

               if(!list.isEmpty()&&isDigit(list.last()))
               {
                   var pinjie=c.pinjie(list)

                   var cal=c.calculate(c.getPostOrder(pinjie))
                   cal=cal.toBigDecimal().multiply(0.01.toBigDecimal()).toDouble()
                   var s=expression.text

                   s=s.substring(0,s.length-pinjie[pinjie.size-1].length)
                   var ss=cal.toString()
                   Log.i("222222",ss)
                   s=s+cal
                   expression.text=s
//               expression.append(cal.toString())
//

                   for(i in 0 until pinjie[pinjie.size-1].length)
                   {
                       list.removeAt(list.size-1)
                   }

                   for(i in 0 until ss.length)
                   {
                       list.add(ss[i])
                   }
//                   for(i in 0 until cal.toString().length)
//                   {
//                       list.removeAt(list.size-1)
//                   }
               }

               Log.i("1111111",list.toString())
           }

           R.id.div->{
               if(!list.isEmpty()&&isDigit(list.last()))
               {
                   list.add('/');
                   expression.append("÷")
               }

           }

           R.id.seven->{
               list.add('7')
               expression.append("7")
           }

           R.id.eight->{
               list.add('8')
               expression.append("8")
           }

           R.id.nine->{
               list.add('9')
               expression.append("9")
           }

           R.id.mutiple->{

               if(!list.isEmpty()&&isDigit(list.last()))
               {
                   list.add('*')
                   expression.append("×")
               }


           }

           R.id.four->{
               list.add('4')
               expression.append("4")
           }

           R.id.five->{
               list.add('5')
               expression.append("5")
           }

           R.id.six->{
               list.add('6')
               expression.append("6")
           }

           R.id.subtract->{

               if(!list.isEmpty()&&isDigit(list.last()))
               {
                   list.add('-')
                   expression.append("-")
               }

           }

           R.id.one->{
               list.add('1')
               expression.append("1")
           }

           R.id.two->{
               list.add('2')
               expression.append("2")
           }
           R.id.three->{
               list.add('3')
               expression.append("3")
           }

           R.id.add->{
               if(!list.isEmpty()&&isDigit(list.last()))
               {
                   list.add('+')
                   expression.append("+")
               }

           }

           R.id.zero->{
               list.add('0')
               expression.append("0")
           }

           R.id.point->{
               //如果前一个为数字并且数字里没有小数点，则可以添加小数点
               if(!list.isEmpty()&& isDigit(list.last())&&!c.pinjie(list).last().contains("."))
               {
                   list.add('.')
                   expression.append(".")
               }


           }

           R.id.equal->{
               if(!list.isEmpty())
               {
                   var m= c.pinjie(list);
                   var post= c.getPostOrder(m)


//               println(m)
//               print(post)
                   //如果最后一个为字符，则去掉

                   if(c.isOper(expression.text.toString().last().toString()))
                   {
                       var s=expression.text
                       s=s.substring(0,s.length-1)
                       expression.text=s
                   }
                   var res=c.calculate(post).toString()

                   //如果最后结果为X.0的格式，则替换为X
                   if(res.endsWith(".0"))
                   {
                       result.text= res.substringBefore(".0")
                   }else
                   {
                       result.text=res
                   }
                   var fadein= AnimationUtils.loadAnimation(this,R.anim.fade_in)
                   var fadeout=AnimationUtils.loadAnimation(this,R.anim.fade_out)
//                   expression.startAnimation(fadeout)
                   result.startAnimation(fadein)


//               print( c.calculate(post))
               }
//               val list= mutableListOf('1','3','3','.','5','-','2','/','2')

           }
       }
    }



}
